1

In the session, we calculated the distances to the closest hospitals located within North-Rhine Westphalia (NRW). Still, we did not show how to subset the original file, which contains all hospitals in Germany.

Subset the data file yourself by relying on the spatial information of the file hospital_points.csv and the polygon of North-Rhine Westphalia. How many hospitals are located within the borders of NRW?
You need two shapefiles for that: the point layer hospital_points in the ./data folder and a shapefile of NRW. For NRW, you can use the osmdata syntax.

The default of sf::st_join() will leave you with a ‘left-join’ and returns a data object with all hospitals and matching district information for those which are located within NRW. You can reset the option to perform an ‘inner-join’ and keep only the observation which lay within the predefined area (sf::st_join(x , y, join = "", left = FALSE)).

# load hospitals
hospitals <- 
  read.csv(
    "./data/hospital_points.csv", 
    header = TRUE, 
    fill = TRUE, 
    sep = ","
  ) %>%
  sf::st_as_sf(coords = c("X", "Y"), crs = 3035)


#  use the OSM function
nrw <-
  osmdata::getbb(
    "Nordrhein-Westfalen", 
    format_out = "sf_polygon"
  ) %>% 
  .$multipolygon %>% 
  sf::st_transform(3035)

# spatial join
nrw_hospitals <-
  hospitals %>% 
  sf::st_join(
    # point layer nrw
    nrw, 
    # chose intersect or within
    join = sf::st_intersects,
    # option FALSE will 
    # keep only the hospital 
    # which could be joined
    left = FALSE
  )

nrw_hospitals
## Simple feature collection with 344 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 4039950 ymin: 3058862 xmax: 4277224 ymax: 3246338
## Projected CRS: ETRS89-extended / LAEA Europe
## First 10 features:
##                                                                                              name year beds     ags                geometry
## 319                                                                            Ev. Krankenhaus    2017  568 5111000 POINT (4095599 3127379)
## 320 Fliedner Klinik Ambulanz und Tagesklinik f\xfcr Psychiatrie, Psychotherapie und Psychosomatik 2017    . 5111000 POINT (4096285 3128719)
## 321                          Johanniter-Tagesklinik Klinik f\xfcr Psychiatrie und Psychotherapie  2017    . 5111000 POINT (4101713 3121737)
## 322                                                       Krankenanstalten Florence Nightingale   2017  568 5111000 POINT (4094348 3137304)
## 323                                                           Krankenhaus M\xf6rsenbroich-Rath    2017  718 5111000 POINT (4099466 3133197)
## 324         LVR-Klinikum D\xfcsseldorf Klinikum der Heinrich-Heine- Universit\xe4t D\xfcsseldorf  2017  495 5111000 POINT (4100738 3130402)
## 325                                                                            Marien-Hospital    2017  461 5111000 POINT (4096526 3129923)
## 326                                            Medical Center D\xfcsseldorf -Luisenkrankenhaus-   2017   42 5111000 POINT (4099035 3129691)
## 327                     Paracelsus Klinik Golzheim Fachklinik f\xfcr Urologie und Kinderurologie  2017   81 5111000 POINT (4094841 3132032)
## 328                                                                Sana Kliniken D\xfcsseldorf    2017  490 5111000 POINT (4101452 3130494)
# 344 hospitals in NRW

2

Did the operationalization of health care provision convince you? Don’t you think it might be more important how many hospitals are close to survey respondents? To test this, we want to calculate the number of hospitals per district in North-Rhine Westphalia. Use the syntax below to prep the hospital data.

Earn extra points by counting not only the number of hospitals but also the sum of hospital beds within a district.
nrw_districts <- 
  sf::read_sf("./data/VG250_KRS.shp") %>% 
  sf::st_transform(3035) %>% 
  sf::st_join(nrw, join = sf::st_intersects, left = FALSE)

nrw_hospitals <-  
  nrw_hospitals %>% 
  # beds were character, now numeric
  dplyr::mutate(beds = as.numeric(beds)) %>%
  # replace NAs as zeros for simplification
  replace(is.na(.), 0)
You need a dplyr::as_tibble() data frame to use the functions dplyr::group_by() and dplyr::summarise().
The function dplyr::n() allows summarising the total count of hospitals. sum(beds) for summarizing the bed total per district.
district_hospital_join <-
  nrw_hospitals %>% 
  # join the hospitals 
  # within districts
  sf::st_join(nrw_districts, join = sf::st_within) %>% 
  # use as tibble to perform
  # group by & summarise
  dplyr::as_tibble() %>% 
  dplyr::group_by(AGS) %>% 
  dplyr::summarise(
    hospital_count = dplyr::n(), 
    hospital_bed_count = sum(as.numeric(beds))
  ) %>% 
  # left join the new information
  # to the original data frame
  left_join(nrw_districts, .)
## Joining, by = "AGS"
district_hospital_join 
## Simple feature collection with 73 features and 25 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 4031313 ymin: 2972671 xmax: 4332216 ymax: 3337853
## Projected CRS: ETRS89-extended / LAEA Europe
## # A tibble: 73 × 26
##      ADE    GF   BSG ARS   AGS   SDV_ARS      GEN                 BEZ       IBZ BEM   NBD   SN_L  SN_R  SN_K  SN_V1 SN_V2 SN_G  FK_S3 NUTS  ARS_0 AGS_0 WSK        DEBKG_ID
##    <int> <int> <int> <chr> <chr> <chr>        <chr>               <chr>   <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <date>     <chr>   
##  1     4     4     1 03155 03155 031550011011 Northeim            Landkr…    43 --    ja    03    1     55    00    00    000   K     DE918 0315… 0315… 2009-01-01 DEBKGDL…
##  2     4     4     1 03251 03251 032510012012 Diepholz            Landkr…    43 --    ja    03    2     51    00    00    000   K     DE922 0325… 0325… 2009-01-01 DEBKGDL…
##  3     4     4     1 03252 03252 032520006006 Hameln-Pyrmont      Landkr…    43 --    ja    03    2     52    00    00    000   K     DE923 0325… 0325… 2009-01-01 DEBKGDL…
##  4     4     4     1 03255 03255 032550023023 Holzminden          Landkr…    43 --    ja    03    2     55    00    00    000   K     DE926 0325… 0325… 2009-01-01 DEBKGDL…
##  5     4     4     1 03256 03256 032560022022 Nienburg (Weser)    Landkr…    43 --    ja    03    2     56    00    00    000   K     DE927 0325… 0325… 2009-01-01 DEBKGDL…
##  6     4     4     1 03257 03257 032570035035 Schaumburg          Landkr…    43 --    ja    03    2     57    00    00    000   K     DE928 0325… 0325… 2009-01-01 DEBKGDL…
##  7     4     4     1 03404 03404 034040000000 Osnabrück           Kreisf…    40 --    ja    03    4     04    00    00    000   K     DE944 0340… 0340… 1972-07-01 DEBKGDL…
##  8     4     4     1 03454 03454 034540035035 Emsland             Landkr…    43 --    ja    03    4     54    00    00    000   K     DE949 0345… 0345… 2009-01-01 DEBKGDL…
##  9     4     4     1 03456 03456 034560015015 Grafschaft Bentheim Landkr…    43 --    ja    03    4     56    00    00    000   K     DE94B 0345… 0345… 2009-01-01 DEBKGDL…
## 10     4     4     1 03459 03459 034040000000 Osnabrück           Landkr…    43 --    ja    03    4     59    00    00    000   K     DE94E 0345… 0345… 1998-04-01 DEBKGDL…
## # … with 63 more rows, and 3 more variables: geometry <MULTIPOLYGON [m]>, hospital_count <int>, hospital_bed_count <dbl>